﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Trackball
{
    public partial class WinMain : Form
    {
        public WinMain()
        {
            mInteractor = new TrackBallInteractor();
            mCamera = new Camera();
            mRenderer = new Renderer();
            InitializeComponent();
        }

        private TrackBallInteractor mInteractor;
        private Camera mCamera;
        private Renderer mRenderer;

        private void mGLControl_MouseDown(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    mInteractor.setLeftClicked(true);
                    break;
                case MouseButtons.Middle:
                    mInteractor.setMiddleClicked(true);
                    break;
                case MouseButtons.Right:
                    mInteractor.setRightClicked(true);
                    break;
                default: break;
            }
            mInteractor.setClickPoint(e.X, e.Y);
        }

        private void mGLControl_MouseUp(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    mInteractor.setLeftClicked(false);
                    break;
                case MouseButtons.Middle:
                    mInteractor.setMiddleClicked(false);
                    break;
                case MouseButtons.Right:
                    mInteractor.setRightClicked(false);
                    break;
                default: break;
            }
        }

        private void mGLControl_MouseMove(object sender, MouseEventArgs e)
        {
            mInteractor.setClickPoint(e.X, e.Y);
        }

        private void mGLControl_KeyDown(object sender, KeyEventArgs e)
        {
            float length = 0.0f;

            switch (e.KeyCode)
            {
                case Keys.ControlKey:
                    mInteractor.setSpeed(5.0f);
                    break;
                case Keys.ShiftKey:
                    mInteractor.setSpeed(0.1f);
                    break;
                case Keys.R:
                    mCamera.reset();
                    mInteractor.setCamera(mCamera);
                    break;
                case Keys.T:
                    if (mInteractor.getMotionRightClick() == CameraMotionType.FIRSTPERSON)
                    {
                        mInteractor.setMotionRightClick(CameraMotionType.PAN);
                    }
                    else
                    {
                        mInteractor.setMotionRightClick(CameraMotionType.FIRSTPERSON);
                    }
                    break;
                case Keys.X:
                    length = (mCamera.getEye() - mCamera.getCenter()).Length;
                    mCamera.setEye(length, 0.0f, 0.0f);
                    mCamera.setUp(0.0f, 1.0f, 0.0f);
                    mCamera.update();
                    mInteractor.setCamera(mCamera);
                    break;
                case Keys.Y:
                    length = (mCamera.getEye() - mCamera.getCenter()).Length;
                    mCamera.setEye(0.0f, length, 0.0f);
                    mCamera.setUp(1.0f, 0.0f, 0.0f);
                    mCamera.update();
                    mInteractor.setCamera(mCamera);
                    break;
                case Keys.Z:
                    length = (mCamera.getEye() - mCamera.getCenter()).Length;
                    mCamera.setEye(0.0f, 0.0f, length);
                    mCamera.setUp(1.0f, 0.0f, 0.0f);
                    mCamera.update();
                    mInteractor.setCamera(mCamera);
                    break;
                default: break;
            }
        }

        private void mGLControl_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.ControlKey:
                    mInteractor.setSpeed(1.0f);
                    break;
                case Keys.ShiftKey:
                    mInteractor.setSpeed(1.0f);
                    break;
                default: break;
            }
        }

        private void mGLControl_MouseWheel(object sender, MouseEventArgs e)
        {
            mInteractor.setScrollDirection(e.Delta > 0 ? true : false);
        }

        private void mGLControl_SizeChanged(object sender, EventArgs e)
        {
            mInteractor.setScreenSize(mGLControl.Width, mGLControl.Height);
            mRenderer.resize(mGLControl.Width, mGLControl.Height);
        }

        private void mGLControl_ContextCreated(object sender, OpenGL.GlControlEventArgs e)
        {
            mInteractor.setCamera(mCamera);
            mRenderer.setCamera(mCamera);
            mRenderer.init();
        }

        private void mGLControl_Render(object sender, OpenGL.GlControlEventArgs e)
        {
            mInteractor.update();
            mRenderer.render();
        }

        private void mGLControl_Load(object sender, EventArgs e)
        {

        }

        private void WinMain_Load(object sender, EventArgs e)
        {

        }
    }
}
